import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import {Link} from 'react-router';

import * as Util from '../../common/Util';
import {
  Grid,
  Row,
  Col,
  Input,
  Button,
  Alert,
  Navbar,
  Nav,
  NavItem
} from 'react-bootstrap';

import Notify from '../../common/Notification';
import * as Shell from '../../common/Shell';
import Container from '../../components/Container';
import * as WxUpgradeActions from '../../actions/WxUpgradeActions';

class WxUpgradeApp2 extends React.Component {

  constructor () {
    super();  

    this.deviceInfo = null;
  }

  static propTypes = {
    dispatch: React.PropTypes.func.isRequired
  }

  onGetDeviceInfoSuccess(data){
    if (data && data.channelid && data.deviceId && data.devicetype) {
      this.deviceInfo = data;
    }
  }

  componentDidMount(){
    //获取设备相关信息
    Shell.callShellMethod(
        "getBaiduYunParamsPlugin", 
        this.onGetDeviceInfoSuccess.bind(this)
    );
  }

  handleSubmitClick() {

    let password = this.refs.password.getValue();
    if(!Util.passwordValidate(password)){
      Notify.makeNotify("密码格式不正确");
      return;
    }

    let repassword = this.refs.repassword.getValue();
    if(!Util.passwordValidate(repassword)){
      Notify.makeNotify("确认密码格式不正确");
      return;
    }

    if (password!==repassword) {
      Notify.makeNotify("两次输入密码不匹配");
      return;
    }

    password = Util.RSAEncrypt(password);

    let deviceInfo = this.deviceInfo;

    let registerData = {
      phone : this.props.wxupgrade.phone,
      captcha : this.props.wxupgrade.captcha,
      password,
      deviceid : deviceInfo?deviceInfo.deviceId:"",
      baiduid : deviceInfo?deviceInfo.channelid:"",
      baidu_devicetype : deviceInfo?deviceInfo.devicetype:""
    };

    this.props.wxupgradeAction.wxupgrade2(registerData);
  }

  componentDidUpdate(){
    let { wxupgrade2_status = -1, message = "", phone = "", password = "", khid="" } = this.props.wxupgrade;
    
    if(wxupgrade2_status === -1){
      Notify.makeNotify(message);
    }else if(wxupgrade2_status === 1){

      Notify.makeNotify("完善资料成功");
      let query = { phone : phone };
      setTimeout(function(){
        this.props.history.pushState(null, "/Login", query);
      }.bind(this), 2000);
    }
  }

  render() {
    let { wxupgrade } = this.props;
    return (
        <Container  
          isGoBack={true} 
          isFooter={false}
          isHeader={true} 
          isAuth = {false}
          headerTitle={"资料补充"}>
          
            <div id="account-panel">
                <form className="form-horizontal">
                  <div className="text-center padding-lg">
                      <p className="lead text-primary">
                         { wxupgrade.phone }
                      </p>
                    </div>

                  <div className="form-element">
                    <Input type="password" 
                      ref = "password"
                      defaultValue = ""
                      placeholder = "6-16位数字、字母"
                      maxLength = "16"
                      wrapperClassName="col-xs-12" 
                      addonBefore="密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码"/>
                    <div className="devider"></div>
                    <Input type="password" 
                      ref = "repassword"
                      defaultValue=""
                      placeholder = "请再次输入密码" 
                      maxLength = "16"
                      wrapperClassName="col-xs-12"
                      addonBefore="确认密码"/>
                  </div>

                  <div className="text-center padding-left-lg padding-right-lg">
                    <Button bsStyle="primary" block onClick={this.handleSubmitClick.bind(this)}>确认</Button>  
                  </div>

                </form>

            </div>
                
        </Container>
    );
  }
}

export default connect(state => ({
    wxupgrade : state.wxupgrade
}), dispatch => ({
    wxupgradeAction: bindActionCreators(WxUpgradeActions, dispatch)
}))(WxUpgradeApp2);

